package cn.zc.curriculum.service.service.impl;

import cn.tools.core.VO.PageVO;
import cn.tools.core.exception.BizException;
import cn.tools.core.utils.mybatis.PageUtil;
import cn.tools.dozer.config.DozerUtils;
import cn.zc.curriculum.entity.Dto.InterfaceManagerDto;
import cn.zc.curriculum.entity.InterfaceManager;
import cn.zc.curriculum.service.mapper.InterfaceManagerMapper;
import cn.zc.curriculum.service.service.InterfaceManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author keno
 * @since 2022-02-19
 */
@Service
public class InterfaceManagerServiceImpl extends ServiceImpl<InterfaceManagerMapper, InterfaceManager> implements InterfaceManagerService {
    @Resource
    private DozerUtils dozerUtils;

    @Resource
    private InterfaceManagerMapper interfaceManagerMapper;


    /**
     * 创建轮播图，友链，菜单
     * @param interfaceManagerDto
     */
    @Override
    public void createInterfaceManager(InterfaceManagerDto interfaceManagerDto) {
        InterfaceManager map = dozerUtils.map(interfaceManagerDto, InterfaceManager.class);
        if(!save(map)){
            throw new BizException("创建失败");
        }
    }

    /**
     * 修改轮播图，友链，菜单
     * @param interfaceManagerDto
     */
    @Override
    public void uploadInterfaceManager(InterfaceManagerDto interfaceManagerDto) {
        InterfaceManager map = dozerUtils.map(interfaceManagerDto, InterfaceManager.class);
        if(!updateById(map)){
            throw new BizException("修改失败");
        }
    }

    /**
     * 删除数据
     * @param id
     */
    @Override
    public void deleteInterfaceManager(String id) {
        if(!removeById(id)){
            throw new BizException("删除失败");
        }
    }

    /**
     * 根据id与类型查询数据
     * @param pageVO
     * @param id
     * @param type
     * @return
     */
    @Override
    public List<InterfaceManagerDto> findInterfaceManager(PageVO pageVO, String id, Integer type) {
        return interfaceManagerMapper.findInterfaceManager(PageUtil.initPage(pageVO),id,type);
    }


    /**
     * 查询出所有数据
     * @return
     */
    @Override
    public Map<String, List<InterfaceManagerDto>> findAllInterfaceManager() {
        PageVO pageVO = new PageVO();
        pageVO.setPageSize(1000);
        pageVO.setPageSize(1);
        List<InterfaceManagerDto> interfaceManager = interfaceManagerMapper.findInterfaceManager(PageUtil.initPage(pageVO), null, null);

        Map<String, List<InterfaceManagerDto>> map = new HashMap<>();
        ArrayList<InterfaceManagerDto> title = new ArrayList<>();
        ArrayList<InterfaceManagerDto> end = new ArrayList<>();
        ArrayList<InterfaceManagerDto> img = new ArrayList<>();

        for (InterfaceManagerDto manager : interfaceManager){
            if(manager.getType().equals("1")){
                title.add(manager);
            }else if(manager.getType().equals("2")){
                img.add(manager);
            }else{
                end.add(manager);
            }
        }
        map.put("title",title);
        map.put("end",end);
        map.put("img",img);
        return map;
    }
}
